<template >
    <div class="top">
        <div style="display: flex; height: 100%">
            <div class="left">
                <el-input placeholder="输入关键字进行过滤" v-model="filterText"> </el-input>
                <el-tree
                    class="filter-tree"
                    :data="treeData"
                    :props="defaultProps"
                    :filter-node-method="filterNode"
                    @node-click="treenodeClick"
                    ref="tree"
                >
                    <!-- default-expand-all 默认展开-->
                </el-tree>
            </div>
            <div class="right">
                <div class="Button_image">
                    <el-image :src="refresh" style="height:70px;width:110px" @click="refreshs()"></el-image>
                    <el-image :src="add" style="height:70px;width:110px" @click="newchan('新增常量', pid)"></el-image>
                    <el-image :src="deletes" style="height:70px;width:110px" @click="getdelect()"></el-image>
                    <div class="block2">
                        <div class="src4">
                            <!-- <el-image :src="query_box"></el-image> -->
                            <el-input
                                v-model="select"
                                prefix-icon="el-icon-search"
                                style="width: 360px; border-radius: 4px; border: 1px solid gray"
                                placeholder="请输入内容"
                            ></el-input>
                        </div>
                        <div class="src3">
                            <el-image :src="querys" style="height:70px;width:110px" @click="slect"></el-image>
                        </div>
                    </div>
                </div>
                <div class="tabbox">
                    <el-table
                        ref="multipleTable"
                        :data="tableData"
                        border
                        :header-row-style="{ background: '#005ab5', color: '#ffffff' }"
                        tooltip-effect="dark"
                        style="width: 100%; margin: 0 auto"
                        @selection-change="handleSelectionChange"
                    >
                        <el-table-column type="selection" width="70px" align="center"> </el-table-column>
                        <el-table-column prop="constantName" align="center" label="常量名称" min-width="12%"> </el-table-column>
                        <el-table-column prop="addedValue" label="附加值" min-width="12%" align="center"> </el-table-column>
                        <el-table-column prop="serNumber" label="排序" min-width="4%" align="center"> </el-table-column>
                        <el-table-column label="操作" min-width="6%" align="center">
                            <template slot-scope="scope">
                                <div class="src5">
                                    <el-image :src="src6" style="height:40px;width:70px;" @click="newchan('修改常量', scope.row)"></el-image>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="page_border">
                        <div class="pagenum_sys">
                            <p>第{{ pageData.page }}页&nbsp;&nbsp;每页{{ pageData.pageSize }}条&nbsp;&nbsp;共{{ pageData.total }}条</p>
                        </div>
                        <div class="page_sys">
                            <el-button type="text" @click="toPage(1)">首页</el-button>
                            <el-button type="text" @click="prePage()">上一页</el-button>
                            <el-button type="text" @click="nextPage()">下一页</el-button>
                            <el-button type="text" @click="toPage(-1)">末页</el-button>
                            <el-button type="text" style="color: #333">第</el-button>&nbsp;
                            <el-input style="width: 76px" v-model="pageNum" clearable></el-input>&nbsp;
                            <el-button type="text" style="color: #333">页</el-button>
                            <el-button type="text" @click="toPage(pageNum)">转到</el-button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <constant-input :formVisible="formVisible" :formData="formData" @closeDialog="closeDialog()"></constant-input>
        <!-- <el-dialog id="id_eldiaglog" :title="dialog_title" :visible.sync="dialog" width="80%" height="100%">
            <div id="idfarm">
                <iframe id="idfarm" src="" frameborder="0" width="100%" height="300px"></iframe>
            </div>
        </el-dialog> -->
    </div>
</template>

<script>
import '../../../common/jdCommon.css';
import cons from '@/api/system_setting/constant_manager.js';
import ConstantInput from './constant_input';
export default {
    components: { ConstantInput },
    data() {
        return {
            formVisible: false,
            formData: null,
            refresh: require('../../../../assets/imgs/刷新按钮.png'),
            deletes: require('../../../../assets/imgs/删除按钮.png'),
            querys: require('../../../../assets/imgs/查询按钮.png'),
            src6: require('../../../../assets/imgs/修改按钮.png'),
            add: require('../../../../assets/imgs/组 557.png'),
            input: '',
            pageData: {
                page: 1,
                pageSize: 10,
                total: 100
            },
            pageNum: null,
            tableData: [],
            select: '',
            //目录树开始
            filterText: '',
            treeData: [],
            defaultProps: {
                children: 'dtos',
                label: 'proName'
            },
            dialog: false, //弹出框
            dialog_title: '',
            curDeptId: '', //当前选中的部门ID
            curDeptName: '',
            pid: '',
            word: ''
        };
    },
    created() {
        this.getpage();
        this.gettree();
    },
    watch: {
        // 根据关键字筛选树
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods: {
        refreshs() {
            let param = {
                page: this.pageData.page,
                pageSize: this.pageData.pageSize,
                word: '',
                pid: this.curDeptId
            };
            this.$post('sysManage/constantManage/getWord', param).then((res) => {
                this.tableData = res.data.rows;
                this.pageData.total = res.data.total;
                this.$message.success('刷新成功!');
            });
        },
        // 分页请求
        getpage() {
            let param = {
                page: this.pageData.page,
                pageSize: this.pageData.pageSize,
                word: '',
                pid: this.curDeptId
            };
            this.$post('sysManage/constantManage/getWord', param).then((res) => {
                this.tableData = res.data.rows;
                this.pageData.total = res.data.total;
            });
        },
        // 树形请求
        gettree() {
            this.$post('sysManage/constantManage/getTree').then((res) => {
                this.treeData = res.data;
            });
        },
        // 搜索框请求
        slect() {
            let param = {
                page: this.pageData.page,
                pageSize: this.pageData.pageSize,
                word: this.select,
                pid: this.curDeptId
            };
            this.$post('sysManage/constantManage/getWord', param).then((res) => {
                this.tableData = res.data.rows;
                this.pageData.total = res.data.total;
            });
        },
        // 删除请求
        getdelect() {
            let currentSelect = this.$refs.multipleTable.selection;
            currentSelect.forEach((item, index) => {
                this.$post('sysManage/constantManage/delete/' + item.id).then((res) => {
                    this.$message.success('删除成功!');
                });
            });
            this.getpage();
            this.gettree();
        },
        // 新增常量弹框
        newchan(stype, pid) {
            if (this.curDeptId == '') {
                this.$message.error('请先选择部门');
                return false;
            } else {
                this.formVisible = true;
                this.formData = pid;
            }
            this.getpage();
            this.gettree();
        },
        newchans(stype, row) {
            if (this.curDeptId == '') {
                this.$message.error('请先选择部门');
                return false;
            } else {
                this.formVisible = true;
                this.formData = pid;
            }
            this.getpage();
            this.gettree();
        },
        treenodeClick(data, node, tnode) {
            // this.curDeptId = node.id;
            this.curDeptId = data.id;
            this.pid = data.id;
            this.word = data.proName;

            this.pageData.page = 1;
            this.getpage();
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        filterNode(value, treeData) {
            if (!value) return true;
            return treeData.proName.indexOf(value) !== -1;
        },
        prePage() {
            // 上一页
            let page = this.pageData.page;
            if (page === 1) return;
            page--;
            this.pageData.page = page;
            // this.search();
        },
        nextPage() {
            // 下一页
            let total = Math.ceil(this.pageData.total / this.pageData.pageSize);
            let page = this.pageData.page;
            if (page === total) return;
            page++;
            this.pageData.page = page;
            // this.search();
        },
        toPage(num) {
            // 走一页
            let total = Math.ceil(this.pageData.total / this.pageData.pageSize);

            if (num > total || num === -1) {
                num = total;
                // this.formVisible = true
            } else if (num < 1) {
                num = 1;
            }

            this.pageData.page = num;
            // this.search();
        },
        closeDialog() {
            this.formVisible = false;
            this.formData = null;
        },
          changeLayout (type) {
            let $ = this.$
            if (type === 1) {
                $('.right_box').show()
                $('.left-2').css('cssText', 'position: relative;width: 100%;')
            } else if (type === 2) {
                $('.right_box').hide()
                $('.left-2').css('cssText', 'position: absolute;width: 98%;')
            }
        }
    },
      mounted(){
         this.changeLayout(2)
    },
     destroyed() {
        this.changeLayout(1)
    }
};
function dialogFromSrc(id) {
    $('#idfarm').html(
        '<iframe id="idfarm"  frameborder="0" width="100%" height="300px" src="/?r=' +
            Math.random() +
            '/#/constant_input" data-id="' +
            id +
            '" ></iframe>'
    );
}
</script>

<style scoped>
.left {
    width: 20%;
    height: 97.5%;
    float: left;
    background-color: white;
    border-radius: 10px;
    box-shadow: 2px 2px 5px gray;
    overflow: auto;
    margin-left: 2%;
    margin-top: 1.5%;
}
.right {
    width: 80%;
    height: 100%;
    float: right;
    margin-right: 10px;
}
.Button_image {
    margin-top: 1%;
    margin-left: 2%;
}
.src4 {
    margin-top: 3%;
    float: left;
}
.src3 {
    float: right;
}
.src5 {
    margin: 0 auto;
}
/deep/.el-image__inner {
    vertical-align: top;
    margin-top: -6px !important;
}
</style>